package com.builder.settings;

import com.gooddies.utils.KeyValue;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 * @author sad
 */
public class PropertiesTableModel extends AbstractTableModel {

    private final List<KeyValue<String, String>> values;
    private final String[] columnNames = new String[]{"Key", "Value"};

    public PropertiesTableModel(List<KeyValue<String, String>> values) {
        this.values = values;
    }

    public List<KeyValue<String, String>> getValues() {
        return values;
    }
   

    public void addNew() {
        values.add(new KeyValue<String, String>("", ""));
        fireTableDataChanged();
    }

    public void removeRow(int index) {
        values.remove(index);
        fireTableDataChanged();
    }

    public KeyValue<String, String> getRow(int row) {
        return values.get(row);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        String v = (String) aValue;
        KeyValue<String, String> value = values.get(rowIndex);
        switch (columnIndex) {
            case 0:
                value.setKey(v);
                break;
            case 1:
                value.setValue(v);
                break;
        }
    }

    @Override
    public int getRowCount() {
        return values.size();
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        KeyValue<String, String> value = values.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return value.getKey();
            case 1:
                return value.getValue();
        }

        throw new IllegalStateException("Properties table model has only 2 columns");
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }
}
